package cn.homework.web.servlet;

import cn.homework.domain.Idea;
import cn.homework.service.FeedbackService;
import cn.homework.service.impl.FeedbackServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author ACT
 */
@WebServlet("/feedbackServlet")
public class FeedbackServlet extends HttpServlet {
    private FeedbackService service = new FeedbackServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String demand = request.getParameter("demand");
//        System.out.println("demandComments："+ demand);
        switch (demand) {
            case "showAll": {
                feedbackShow(request, response);
                break;
            }
            case "comments": {
                publishFeedback(request, response);
                break;
            }
            case "reply": {
                replyFeedback(request, response);
                break;
            }
            case "show": {
                feedbackBackstageShow(request, response);
                break;
            }
            case "delete": {
                feedbackDel(request, response);
                break;
            }
            case "detail": {
                feedbackDetail(request, response);
                break;
            }
            case "query": {
                feedbackFindByCondition(request, response);
                break;
            }
            default:
                break;
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    /**
     * 查询所有留言并显示
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void feedbackShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Idea> ideasAllList = service.findAllFeedback();
        HttpSession session = request.getSession();
        session.setAttribute("ideaAllList", ideasAllList);
        response.sendRedirect(request.getContextPath() + "/CommodityFeedback.jsp");
    }

    /**
     * 发表留言
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void publishFeedback(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String message = request.getParameter("new_message");
        String name = request.getParameter("customer");
        service.addPublishFeedback(message, name);
        feedbackShow(request, response);
    }

    /**
     * 回复留言
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void replyFeedback(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("replyFeedback start ...");
        String message = request.getParameter("re_message");
        String name = request.getParameter("customer");
        String id = request.getParameter("feedbackId");
//        System.out.println("id；" + id);
//        System.out.println("message:" + message);
//        System.out.println("name:" + name);
        service.addReplyFeedback(message, name, id);
        feedbackShow(request, response);
    }

    /**
     * 在后台显示留言信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void feedbackBackstageShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Idea> ideasAllList = service.findAllFeedback();
        HttpSession session = request.getSession();
        session.setAttribute("ideaAllList", ideasAllList);
        response.sendRedirect(request.getContextPath() + "/BackstageFeedbackShow.jsp");
    }

    /**
     * 根据id删除留言
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void feedbackDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        service.delFeedback(id);
        feedbackBackstageShow(request, response);
    }

    /**
     * 显示留言详细信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void feedbackDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Idea feedback = service.feedbackDetailById(id);
        HttpSession session = request.getSession();
        session.setAttribute("feedbackDetail", feedback);
        response.sendRedirect(request.getContextPath() + "/BackstageFeedbackDetails.jsp");
    }

    /**
     * 根据条件查找留言
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void feedbackFindByCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        List<Idea> ideaList = service.findFeedbackByCondition(map);
        HttpSession session = request.getSession();
        session.setAttribute("ideaList", ideaList);
        response.sendRedirect(request.getContextPath() + "/BackstageFeedbackQuery.jsp");
    }
}
